package com.atguigu.juc.day03.jmm;

import java.util.concurrent.CountDownLatch;

public class Demo01AtomicTest {

    private int number = 0;

    public synchronized void doAdd() {
        number++;
    }

    public int getNumber() {
        return number;
    }

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(1);

        Demo01AtomicTest demo = new Demo01AtomicTest();

        new Thread(()->{
            long beginTime = System.currentTimeMillis();
            for (int i = 0; i < 10000000; i++) {
                demo.doAdd();
            }
            long endTime = System.currentTimeMillis();
            long usedTime = endTime - beginTime;
            System.out.println("usedTime = " + usedTime + "ms");
            countDownLatch.countDown();
        }, "thread-a").start();

//        new Thread(()->{
//            long beginTime = System.currentTimeMillis();
//            for (int i = 0; i < 10000000; i++) {
//                demo.doAdd();
//            }
//            long endTime = System.currentTimeMillis();
//            long usedTime = endTime - beginTime;
//            System.out.println("usedTime = " + usedTime + "ms");
//            countDownLatch.countDown();
//        }, "thread-b").start();

        countDownLatch.await();
        int numberCurrent = demo.getNumber();
        System.out.println("numberCurrent = " + numberCurrent);
    }

}
